package com.aliyun.messagemgt.repository;

import com.aliyun.messagemgt.common.dto.send.PushChannelInfo;
import com.aliyun.messagemgt.converter.PushChannelConverter;
import com.aliyun.messagemgt.domain.repository.PushChannelRepository;
import com.aliyun.messagemgt.dto.ChannelEmailParam;
import com.aliyun.messagemgt.dto.PushChannelAdd;
import com.aliyun.messagemgt.dto.PushChannelList;
import com.aliyun.messagemgt.dto.query.PushChannelQuery;
import com.aliyun.messagemgt.entities.ChannelEmailParamEntity;
import com.aliyun.messagemgt.entities.MsgSourceChannel;
import com.aliyun.messagemgt.entities.PushChannelEntity;
import com.aliyun.messagemgt.repository.mappers.ChannelEmailParamMapper;
import com.aliyun.messagemgt.repository.mappers.MsgSourceChannelMapper;
import com.aliyun.messagemgt.repository.mappers.PushChannelMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@Service
public class PushChannelMyBatisRepository implements PushChannelRepository {


    @Autowired
    private PushChannelMapper pushChannelMapper;

    @Autowired
    private ChannelEmailParamMapper channelEmailParamMapper;

    @Autowired
    private MsgSourceChannelMapper msgSourceChannelMapper;
    /**
     * 通过描述查询推送渠道
     *
     * @param pushChannelAdd@return
     */
    @Override
    public int selectByDescription(PushChannelAdd pushChannelAdd) {
        return pushChannelMapper.selectByDescription(pushChannelAdd);
    }

    /**
     * 插入推送渠道
     *
     * @param pushChannelAdd
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPushChannel(PushChannelAdd pushChannelAdd) {
        int data = pushChannelMapper.insertPushChannel(pushChannelAdd);
        PushChannelEntity pushChannelEntity = pushChannelMapper.queryChannelCodeById(pushChannelAdd.getChannelCode());
        if (pushChannelAdd.getChannelType() != 10){
            return data;
        }
        List<ChannelEmailParam> emailParams = pushChannelAdd.getEmailParams();
        if (emailParams == null){
            return data;
        }
        if (emailParams.size() > 0){
            for (ChannelEmailParam emailParam : emailParams) {
                ChannelEmailParamEntity emailParamEntity = new ChannelEmailParamEntity();
                BeanUtils.copyProperties(pushChannelAdd,emailParamEntity);
                emailParamEntity.setPushChannelId(pushChannelEntity.getPushChannelId());
                emailParamEntity.setAttributeCode(emailParam.getAttributeCode());
                emailParamEntity.setAttributeValue(emailParam.getAttributeValue());
                emailParamEntity.setIsDeleted(emailParam.getIsDeleted());
                channelEmailParamMapper.insert(emailParamEntity);
            }
        }
        return data;
    }

    /**
     * 列出推送渠道列表
     *
     * @param pushChannelQuery
     * @return
     */
    @Override
    public List<PushChannelList> listPushChannel(PushChannelQuery pushChannelQuery) {
        List<PushChannelEntity> pushChannelEntityList = pushChannelMapper.listPushChannel(pushChannelQuery);
        List<PushChannelList> pushChannelList = PushChannelConverter.INSTANCE.pushChannelEntityList2pushChannelList(pushChannelEntityList);
        return pushChannelList;
    }

    /**
     * 更新推送渠道
     *
     * @param pushChannelQuery
     * @return
     */
    @Override
    public int topPushChannel(PushChannelQuery pushChannelQuery) {
        return pushChannelMapper.topPushChannel(pushChannelQuery);
    }

    /**
     * 删除推送渠道
     *
     * @param pushChannelQuery
     * @return
     */
    @Override
    public int delPushChannel(PushChannelQuery pushChannelQuery) {
        return pushChannelMapper.delPushChannel(pushChannelQuery);
    }

    /**
     * 修改推送渠道
     *
     * @param pushChannelAdd
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int modPushChannel(PushChannelAdd pushChannelAdd) {
        int data = pushChannelMapper.modPushChannel(pushChannelAdd);
        if (pushChannelAdd.getChannelType() != 10){
            return data;
        }
        List<ChannelEmailParam> emailParams = pushChannelAdd.getEmailParams();
        if (emailParams == null){
            return data;
        }
        if (emailParams.size() > 0){
            for (ChannelEmailParam emailParam : emailParams) {
                ChannelEmailParamEntity paramEntity = new ChannelEmailParamEntity();
                BeanUtils.copyProperties(pushChannelAdd,paramEntity);
                paramEntity.setAttributeCode(emailParam.getAttributeCode());
                paramEntity.setAttributeValue(emailParam.getAttributeValue());
                paramEntity.setIsDeleted(emailParam.getIsDeleted());
                ChannelEmailParamEntity emailParamEntity = channelEmailParamMapper.selectById(emailParam.getId());
                if (emailParamEntity == null){
                    channelEmailParamMapper.insert(paramEntity);
                }else {
                    channelEmailParamMapper.updateById(paramEntity);
                }
            }
        }

        return data;
    }

    /**
     * 查询渠道详情
     *
     * @param pushChannelId
     * @return
     */
    @Override
    public PushChannelList queryChannelDetail(Long pushChannelId) {
        PushChannelEntity pushChannelEntity = pushChannelMapper.queryChannelDetail(pushChannelId);
//        PushChannelList pushChannelList = PushChannelConverter.INSTANCE.pushChannelEntity2pushChannelList(pushChannelEntity);
        PushChannelList pushChannelList = new PushChannelList();
        BeanUtils.copyProperties(pushChannelEntity,pushChannelList);
        if (pushChannelEntity.getChannelType() != 10){
            return pushChannelList;
        }
        QueryWrapper<ChannelEmailParamEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("push_channel_id",pushChannelId);
        queryWrapper.eq("is_deleted","N");
        //根据渠道id查询相应的集合
        List<ChannelEmailParamEntity> emailParamEntities = channelEmailParamMapper.selectList(queryWrapper);
        List<ChannelEmailParam> emailParams = new ArrayList<>();
        for (ChannelEmailParamEntity emailParamEntity : emailParamEntities) {
            ChannelEmailParam channelEmailParam = new ChannelEmailParam();
            BeanUtils.copyProperties(emailParamEntity,channelEmailParam);
            emailParams.add(channelEmailParam);
        }
        pushChannelList.setParams(emailParams);
        return pushChannelList;
    }

    /**
     * 先查询置顶渠道
     *
     * @param pushChannelQuery
     * @return
     */
    @Override
    public List<PushChannelList> queryTopChannel(PushChannelQuery pushChannelQuery) {
        List<PushChannelEntity> pushChannelEntityList = pushChannelMapper.queryTopChannel(pushChannelQuery);
        List<PushChannelList> pushChannelList = PushChannelConverter.INSTANCE.pushChannelEntityList2pushChannelList(pushChannelEntityList);
        return pushChannelList;
    }

    @Override
    public List<PushChannelList> listPushChannelNoTop(PushChannelQuery pushChannelQuery) {
        List<PushChannelEntity> pushChannelEntityList = pushChannelMapper.listPushChannelNoTop(pushChannelQuery);
        List<PushChannelList> pushChannelList = PushChannelConverter.INSTANCE.pushChannelEntityList2pushChannelList(pushChannelEntityList);
        return pushChannelList;
    }

    @Override
    public List<PushChannelInfo> listChannelById(List<Integer> channelIds) {
        return pushChannelMapper.listChannelById(channelIds);
    }

    @Override
    public List<PushChannelInfo> listAllChannel() {
        return pushChannelMapper.listAllChannel();
    }

    @Override
    public PushChannelInfo queryPushChannelInfoByChannelId(Integer channelId) {
        return pushChannelMapper.queryPushChannelInfoByChannelId(channelId);
    }

    @Override
    public Integer updateChannelOffMsg(Integer pushChannelId, Integer offMsg) {
        return pushChannelMapper.updateChannelOffMsg(pushChannelId,offMsg);
    }

    /**
     * 启用/禁用
     *
     * @param pushChannelQuery
     * @return
     */
    @Override
    public int updateStatus(PushChannelQuery pushChannelQuery) {
        return pushChannelMapper.updateStatus(pushChannelQuery);
    }

    @Override
    public int queryChannelBychatId(String chatId) {
        return pushChannelMapper.queryChannelBychatId(chatId);
    }

    @Override
    public List<PushChannelList> queryByChannelCode(String channelCode) {
        List<PushChannelEntity> pushChannelEntityList = pushChannelMapper.queryByChannelCode(channelCode);
        List<PushChannelList> pushChannelList = PushChannelConverter.INSTANCE.pushChannelEntityList2pushChannelList(pushChannelEntityList);
        return pushChannelList;
    }

    @Override
    public List<PushChannelList> queryByChannelName(String description) {
        List<PushChannelEntity> pushChannelEntityList = pushChannelMapper.queryByChannelName(description);
        List<PushChannelList> pushChannelList = PushChannelConverter.INSTANCE.pushChannelEntityList2pushChannelList(pushChannelEntityList);
        return pushChannelList;
    }

    @Override
    public int selectByCode(PushChannelAdd pushChannelAdd) {
        return pushChannelMapper.selectByCode(pushChannelAdd);
    }

    @Override
    public List<PushChannelInfo> getPushChannelByMsgSourceId(Long msgSourceId) {
        QueryWrapper<MsgSourceChannel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("msg_source_id",msgSourceId);
        queryWrapper.eq("is_deleted","N");
        List<MsgSourceChannel> msgSourceChannelList = msgSourceChannelMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(msgSourceChannelList)){
            List<Integer> pushChannelIdList = msgSourceChannelList.stream().map(msgSourceChannel -> msgSourceChannel.getPushChannelId().intValue()).collect(Collectors.toList());
            return this.listChannelById(pushChannelIdList);
        }
        return null;
    }


}
